package org.example.controller;

import org.example.config.ThreadPoolExecutor;
import org.example.result.Result;
import org.example.service.UserRoleService;
import org.example.vo.CapacityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @BelongsProject：blockchain-APTrace
 * @CreateTime 2024-06-06-17:09
 * @Author SweetPig
 */
@RestController
@RequestMapping("echarts")
public class EchartsController {


    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserRoleService userRoleService;

    @RequestMapping("index")
    public Result indexQuery() throws ExecutionException, InterruptedException {
        Map<String, Object> map = new HashMap<>();

        ThreadPoolTaskExecutor executor = threadPoolExecutor.threadPoolTaskExecutor();
        Future<?> online = executor.submit(() -> {
            List<Object> list = new ArrayList<>();
            //获取在在线用户
            Set keys = redisTemplate.keys("online_user_*");
            for (Object key : keys) {
                Object o = redisTemplate.opsForValue().get(key);
                list.add(o);
            }
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("size", keys.size());
            hashMap.put("list", list);
            return hashMap;
        });


        //获取各个仓库容量
        Future<?> capacity = executor.submit(() -> userRoleService.getCapacity());
        //材料仓库
        Future<?> material = executor.submit(() -> userRoleService.getMaterial());
        Object houseCapacity = capacity.get();
        Object materialNum = material.get();
        map.put("online", online.get());
        map.put("houseCapacity", houseCapacity);
        map.put("materialNum", materialNum);
        return Result.success(map);
    }


}
